package com.lagou;

import com.lagou.service.UserServiceImpl;
import com.lagou.zk.MyZkClient;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@SpringBootApplication(scanBasePackages = "com.lagou")
public class ServerBoot implements ApplicationContextAware {

    private static ApplicationContext context;

    public static String ip="127.0.0.1";

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ServerBoot.class, args);
        String ip="127.0.0.1";
        int port=8089;
        MyZkClient myZkClient = (MyZkClient)context.getBean("MyZkClient");
        myZkClient.setIp(ip);
        myZkClient.setPort(port);
        myZkClient.initialize();
        //启动服务器
        UserServiceImpl.startServer(ip,port);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context=applicationContext;
    }

//    private static void bindZK(String ip, int port){
//        String node = String.format(ip+":"+port);
//
//        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);
//
//        if (!zkClient.exists("/lg-zkClient")){
//            zkClient.createPersistent("/lg-zkClient");
//        }
//        zkClient.createEphemeral("/lg-zkClient/"+node);
//    }
}
